package cn.iocoder.yudao.module.vote.dal.mysql.voterecord;

import java.util.*;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.vote.dal.dataobject.voteinfo.VoteInfoDO;
import cn.iocoder.yudao.module.vote.dal.dataobject.voterecord.VoteRecordDO;
import cn.iocoder.yudao.module.vote.dal.dataobject.voteuserresult.VoteUserResultDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.vote.controller.admin.voterecord.vo.*;

/**
 * 投票记录 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface VoteRecordMapper extends BaseMapperX<VoteRecordDO> {

    default PageResult<VoteRecordDO> selectPage(VoteRecordPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<VoteRecordDO>()
                .likeIfPresent(VoteRecordDO::getIp, reqVO.getIp())
                .eqIfPresent(VoteRecordDO::getVoteId, reqVO.getVoteId())
                .likeIfPresent(VoteRecordDO::getVoteName, reqVO.getVoteName())
                .eqIfPresent(VoteRecordDO::getVoteCount, reqVO.getVoteCount())
                .eqIfPresent(VoteRecordDO::getVotePersonType, reqVO.getVotePersonType())
                .eqIfPresent(VoteRecordDO::getVoteType, reqVO.getVoteType())
                .betweenIfPresent(VoteRecordDO::getCreateTime, reqVO.getBeginCreateTime(), reqVO.getEndCreateTime())
                .notIn(CollUtil.isNotEmpty(reqVO.getNoInVoteType()), VoteRecordDO::getVoteType,reqVO.getNoInVoteType())
                .orderByDesc(VoteRecordDO::getId));
    }

    default List<VoteRecordDO> selectList(VoteRecordExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<VoteRecordDO>()
                .eqIfPresent(VoteRecordDO::getIp, reqVO.getIp())
                .eqIfPresent(VoteRecordDO::getVoteId, reqVO.getVoteId())
                .likeIfPresent(VoteRecordDO::getVoteName, reqVO.getVoteName())
                .eqIfPresent(VoteRecordDO::getVoteType, reqVO.getVoteType())
                .eqIfPresent(VoteRecordDO::getVoteCount, reqVO.getVoteCount())
                .eqIfPresent(VoteRecordDO::getVotePersonType, reqVO.getVotePersonType())
                .betweenIfPresent(VoteRecordDO::getCreateTime, reqVO.getBeginCreateTime(), reqVO.getEndCreateTime())
                .orderByDesc(VoteRecordDO::getId));
    }

    default Long countByCreater(Long userId,Integer voteId) {
        return selectCount(new LambdaQueryWrapper<VoteRecordDO>()
                .eq(VoteRecordDO::getCreator, userId)
                .eq(VoteRecordDO::getVoteId,voteId)
        );
    }

    default Long countByIp(String ip,Integer voteId) {
        return selectCount(new LambdaQueryWrapper<VoteRecordDO>()
                .eq(VoteRecordDO::getIp, ip)
                .eq(VoteRecordDO::getVoteId,voteId)
        );
    }

    default int delByVoteId(Integer voteId) {
        return delete(new LambdaQueryWrapperX<VoteRecordDO>()
                .eq(VoteRecordDO::getVoteId,voteId)
        );
    }

}
